{* 1. ДИАЛОГИ *}
<div class="dialog" id="manager">
    <form id="manager-form">
	<input type="hidden" name="name" id="dictionary" value="{$smarty.request.name}"/>

	<table align="center">
	    <tr>
		<td align="right"><strong>ID#</strong></td>
		<td><input type="text" name="id" id="manager-id"/></td>
	    </tr>
	    <tr>
		<td align="right"><strong>Логин:</strong></td>
		<td><input type="text" id="manager-login" name="login"/></td>
	    </tr>
	    <tr>
		<td align="right"><strong>ФИО:</strong></td>
		<td><input type="text" id="manager-name" name="manager_name"/></td>
	    </tr>
	    <tr>
		<td align="right"><strong>Пароль:</strong></td>
		<td><input type="text" id="manager-password" name="password"/></td>
	    </tr>
	    <tr>
		<td align="right"><strong>Начало раб.дня:</strong></td>
		<td><input type="text" id="manager-starttime" class="time" name="start_time"/></td>
	    </tr>
	    <tr>
		<td align="right"><strong>Внутренний номер:</strong></td>
		<td><input type="text" id="manager-phone" name="internal_number"/></td>
	    </tr>
	    <tr>
		<td align="right"><strong><label for="is_admin">Администратор:</label></strong></td>
		<td><input type="checkbox" id="is_admin" name="is_admin"/></td>
	    </tr>
	    <tr>
		<td align="right"><strong><label for="is_deleted">Удален:</label></strong></td>
		<td><input type="checkbox" id="is_deleted" name="is_deleted"/></td>
	    </tr>
	</table>
    </form>
</div>

<div class="dialog" id="managers-starttime-dialog">
    <center>
	<input type="text" class="time" id="managers-starttime"/>
    </center>
</div>

{* 2. СКРИПТЫ-ОБРАБОТЧИКИ *}
<script type="text/javascript">
<!--{literal}
$(document)
    .ready
    (
	function()
	{
	    $('table.grid#datagrid')
		.after('<input type="button" value="Удалить" id="managers-delete" style="display:none"/>')
		.after('<input type="button" value="Установить начало рабочего дня" id="managers-set-starttime" style="display:none"/>')
		.jqGrid
		({
		    datatype:	'local',
		    colNames:	['Логин','ФИО','Внутренний номер','Адм.','Удален'],
		    colModel:
		    [
			{name:'login',width:20},
			{name:'manager_name',width:50},
			{name:'internal_number',width:10},
			{name:'is_admin',width:10,formatter:'checkbox'},
			{name:'is_deleted',width:10,formatter:'checkbox'}
		    ],
		    width:	$(document).width() * 0.75,
		    height:	$(document).height()* 0.6,
		    rownumbers:	true,
		    pager:	'datagrid-p',
		    multiselect:	true,
		    onSelectRow:	function(rowid)
					{
					    if ( $(this).getGridParam('selarrrow').length > 0 )
						{ $('input:button[id^=managers]').show(); }
					    else
						{ $('input:button[id^=managers]').hide(); }
					},
		    ondblClickRow:	function(rowid)
		    {
			$.get
			(
			    'dictionaries.xml',
			    {
				id:		rowid,
				name:	'manager'
			    },
			    function(xml)
			    {
				$('div.dialog#manager')
				    .find('input:text#manager-id')
					.val( $(xml).find('field[name=id]').text() )
					.attr('readonly',true)
					.attr('disabled',false)
					.removeClass('ui-state-disabled')
					.end()
				    .find('input:text#manager-login')
					.val( $(xml).find('field[name=login]').text() )
					.attr('disabled',false)
					.removeClass('ui-state-disabled')
					.end()
				    .find('input:text#manager-name')
					.val( $(xml).find('field[name=manager_name]').text() )
					.attr('disabled',false)
					.removeClass('ui-state-disabled')
					.end()
				    .find('input:text#manager-password')
					.val( $(xml).find('field[name=password]').text() )
					.attr('disabled',false)
					.removeClass('ui-state-disabled')
					.end()
				    .find('input:text#manager-starttime')
					.val( $(xml).find('field[name=start_time]').text() )
					.attr('disabled',false)
					.removeClass('ui-state-disabled')
					.focus()
					.end()
				    .find('input:text#manager-phone')
					.val( $(xml).find('field[name=internal_number]').text() )
					.attr('disabled',false)
					.removeClass('ui-state-disabled')
					.end()
				    .find('input:checkbox#is_admin')
					.attr('checked', $(xml).find('field[name=is_admin]').text()==1 ? true : false)
					.attr('disabled',false)
					.removeClass('ui-state-disabled')
					.end()
				    .find('input:checkbox#is_deleted')
					.attr('checked', $(xml).find('field[name=is_deleted]').text()==1 ? true : false)
					.attr('disabled',false)
					.removeClass('ui-state-disabled');
			    },
			    'xml'
			);

			$('div.dialog#manager')
			    .find('input')
				.filter(':text')
				.val('')
				.end()
			    .filter(':checkbox')
				.attr('checked',false)
				.end()
				.addClass('ui-state-disabled')
				.filter(':not(:hidden)')
				    .attr('disabled',true)
				    .end()
				.end()
				.dialog('open');
		    }
		});

	    $('div.dialog#manager')
		.dialog
		(
		    'option',
		    {
			title:	'Редактирование пользователя',
			width:	$(document).width() * 0.3,
			resizable:	false,
			modal:	true,
			height:	320,
			buttons:
			{
			    'OK':	function(){$('form#manager-form').submit();}
			}
		    }
		);

	    $('div.dialog#managers-starttime-dialog')
		.dialog
		(
		    'option',
		    {
			title:	'Установить начало рабочего дня',
			width:	$(document).width() * 0.2,
			resizable:	false,
			modal:	true,
			height:	120,
			buttons:
			{
			    'OK':	function()
					{
					    if ( $('table.grid#datagrid').getGridParam('selarrrow').length>0 )
					    {
						$.post
						(
						    'dictionaries.xml',
						    {
							name:		'manager',
							do:		'set_time',
							start_time:	$('input:text#managers-starttime').val(),
							managers:	$('table.grid#datagrid').getGridParam('selarrrow')
						    },
						    function(xml)
						    {
							if ( $(xml).find('result').attr('status') != 200 )
							    { alert($(xml).find('result').text()); }
							$('div.dialog#managers-starttime-dialog').dialog('close');
						    },
						    'xml'
						);
					    } else { alert('Ничего не выбрано'); }
					}
			}
		    }
		);

	    $('input:button#managers-set-starttime')
		.click(function(){$('div.dialog#managers-starttime-dialog').dialog('open');});

	    $('input:button#managers-delete')
		.click
		(
		    function()
		    {
			if ( $('table.grid#datagrid').getGridParam('selarrrow').length > 0 )
			{
			    if ( window.confirm('Действительно удалить сотрудников?') )
			    {
				$.post
				(
				    'dictionaries.xml',
				    {
					name:	'manager',
					do:		'delete',
					managers:	$('table.grid#datagrid').getGridParam('selarrrow')
				    },
				    function(xml)
				    {
					if ( $(xml).find('result').attr('status') != 200 )
					    { alert($(xml).find('result').text()); }

					reloadGrid();
				    },
				    'xml'
				);
			    }
			} else { alert('Ничего не выбрано!'); }
		    }
		);

	    $('input:text.time')
		.css('text-align','center')
		.mask('99:99:00');

	    $('form#manager-form')
		.submit
		(
		    function()
		    {
			if ( window.confirm('Сохранить?') )
			{
			    var manager = $(this).serialize();

			    $('input:not(:hidden)')
				.attr('disabled',true)
				.addClass('ui-state-disabled');

			    $.post
			    (
				'dictionaries.xml',
				manager,
				function(xml)
				{
				    if ( $(xml).find('result').attr('code') != 200 )
					{ alert($(xml).find('result').text()); }

				    $('div.dialog#manager').dialog('close');
				    reloadGrid();
				},
				'xml'
			    );
			}

			return false;
		    }
		);

	    function reloadGrid()
	    {
		$.get
		(
		    'dictionaries.xml',
		    {
			name:	$('input:hidden#dictionary').val(),
			show_deleted:	($('input:checkbox#show_deleted').attr('checked')?1:0)
		    },
		    function(xml)
		    {
			$('table.grid#datagrid')
			    .setGridParam({datatype:'xmlstring',datastr:xml})
			    .trigger('reloadGrid');
		    },
		    'xml'
		);
	    }

	    reloadGrid();
	}
    );
{/literal}-->
</script>
